package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Category;
import com.itheima.pojo.Spec;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Category> findAll(){
        return this.categoryService.list();
    }

    /**
     * 添加
     * @param category
     * @return
     */
    @PutMapping
    public boolean add(Category category){
        return categoryService.add(category);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<List<Category>> findPage(@PathVariable("page") Long page, @PathVariable("size")Long size) {
        Page categoryPage = new Page<Category>(page, size);

        categoryPage = categoryService.page(categoryPage);
        List<Category> list = categoryPage.getRecords();

        return new Result<List<Category>>(true,null,list);
    }



    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Category findById(@PathVariable("id")Integer id){
        return categoryService.getById(id);
    }

    /**
     * 根据ID更新数据
     * @param id
     * @param category
     * @return
     */
    @PutMapping("/{id}")
    public  boolean update(@PathVariable("id") Integer id,
                           @RequestBody Category category){
        return categoryService.update(category,Wrappers.<Category>lambdaQuery().eq(Category::getId,id));
    }

    /**
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id")Integer id){
        return categoryService.removeById(id);
    }

}
